package com.mbyte.easy.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbyte.easy.admin.entity.Allot;
import com.mbyte.easy.admin.entity.Draft;
import com.mbyte.easy.admin.entity.Ppt;
import com.mbyte.easy.admin.mapper.AllotMapper;
import com.mbyte.easy.admin.mapper.AudioMapper;
import com.mbyte.easy.admin.mapper.DraftMapper;
import com.mbyte.easy.admin.mapper.PptMapper;
import com.mbyte.easy.admin.service.IAllotService;
import com.mbyte.easy.admin.service.ICheckPartnerService;
import com.mbyte.easy.admin.service.ICheckService;
import com.mbyte.easy.admin.service.IRelationService;
import com.mbyte.easy.common.constant.DraftType;
import com.mbyte.easy.common.constant.StateDic;
import com.mbyte.easy.common.constant.SysRole;
import com.mbyte.easy.security.mapper.SysUserMapper;
import com.mbyte.easy.security.mapper.SysUserRolesMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lp
 * @since 2019-12-12
 */
@Slf4j
@Service
public class AllotServiceImpl extends ServiceImpl<AllotMapper, Allot> implements IAllotService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private AudioMapper audioMapper;
    @Autowired
    DraftMapper draftMapper;
    @Autowired
    AllotMapper allotMapper;
    @Autowired
    IRelationService relationService;
    @Autowired
    ICheckService checkService;
    @Autowired
    ICheckPartnerService checkPartnerService;
    @Autowired
    PptMapper pptMapper;
    @Autowired
    private SysUserRolesMapper sysUserRolesMapper;


    @Override
    public Long selectUserIdByUsername(String username) {
        return sysUserMapper.selectUserIdByUsername(username);
    }


    @Override
    public Ppt selectIdByDraftId(Long id){
        return pptMapper.selectIdByDraftId(id);
    }

    /**
     * @Description 稿件校对组长分配稿件首页
     * @param allot
     * @param pageNo
     * @param pageSize
     * @Author myr
     * @return
     **/
    @Override
    public IPage draftCheckLeaderIndex(Draft allot, Integer pageNo, Integer pageSize,String createTimeSpace) {
        QueryWrapper<Draft> qw = new QueryWrapper<>();
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Long rolesId = sysUserRolesMapper.selectByUserId(loginUser).get(0).getRolesId();
        if(rolesId == SysRole.SEVEN_LEADER){
            qw.eq("status_code", StateDic.BLOCK_SEVEN_PASS);//版块负责人审核过,之前的他看不到
            qw.ne("draft_type",DraftType.SVIDEO);
        }else {
            qw.eq("status_code", StateDic.Block_Leader_Pass);//版块负责人审核过,之前的他看不到
        }
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            qw.lambda().between(Draft::getCreateTime,begin,end);
        }
        qw.lambda().orderByDesc(Draft::getCreateTime);
        if(!ObjectUtils.isEmpty(allot.getDraftTitle())) {
            qw = qw.like("draft_title",allot.getDraftTitle());
        }
        if(!ObjectUtils.isEmpty(allot.getDraftType()) && allot.getDraftType() >= DraftType.Image_Text) {
            qw = qw.like("draft_type",allot.getDraftType());
        }
        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),qw);
        log.info("draft fenpei --> {}",draftIPage.getRecords());
        return draftIPage;
    }

    @Override
    public IPage<Draft> draftCheckLeaderIndexTow(Page<Draft> page ,Draft allot, String createTimeSpace) {
        Long loginUserId = checkPartnerService.getCurrentUser().getId();
        Long rolesId = sysUserRolesMapper.selectByUserId(loginUserId).get(0).getRolesId();
        QueryWrapper<Draft> qw = new QueryWrapper<>();
        if(rolesId == SysRole.SEVEN_LEADER ) {
            qw.in("td.status_code", StateDic.DRAFT_ALLOCATED_SEVEN, StateDic.SEVEN_CHECKER_REJECT);
        }else{
            qw.in("td.status_code", StateDic.Draft_Allocated, StateDic.Draft_Checker_Reject);
        }
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            qw.lambda().between(Draft::getCreateTime,begin,end);
        }
        qw.lambda().orderByDesc(Draft::getUpdateTime);
        if(!ObjectUtils.isEmpty(allot.getDraftTitle())) {
            qw = qw.like("td.draft_title",allot.getDraftTitle());
        }

        if(!ObjectUtils.isEmpty(allot.getGroupId())) {
            qw = qw.eq("td.group_id",allot.getGroupId());
        }
        if(!ObjectUtils.isEmpty(allot.getDraftType()) && allot.getDraftType() >= DraftType.Image_Text) {
            qw = qw.eq("td.draft_type",allot.getDraftType());
        }
       return page.setRecords(draftMapper.selectPageList(page,loginUserId,qw));
    }

    /**
     * @Description 稿件校对组长审核首页
     * @param draft
     * @param pageNo
     * @param pageSize
     * @Author myr
     * @return
     **/
    @Override
    public IPage leaderCheckIndex(Draft draft, Integer pageNo, Integer pageSize) {
//        List<SysUser> checkerList = relationService.allotBefore();//获取自己的组员
        QueryWrapper<Draft> qw = new QueryWrapper<>();
//        qw.in("group_id", checkerList.stream().map(SysUser::getId));//获取所有自己分配的稿件
        qw.like("group_id",draft.getGroupId());
        qw.ge("status_code",StateDic.Draft_Allocated);
        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),qw);
        return draftIPage;
    }

}
